<html>
<head><meta charset="utf-8"><title>SpecForElem for other integers · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/SpecForElem.20for.20other.20integers.html">SpecForElem for other integers</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="201274053"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/SpecForElem%20for%20other%20integers/near/201274053" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pickfire <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/SpecForElem.20for.20other.20integers.html#201274053">(Jun 18 2020 at 14:19)</a>:</h4>
<div class="codehilite"><pre><span></span><code><span class="k">impl</span><span class="w"> </span><span class="n">SpecFromElem</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="kt">u8</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="cp">#[inline]</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">from_elem</span><span class="p">(</span><span class="n">elem</span>: <span class="kt">u8</span><span class="p">,</span><span class="w"> </span><span class="n">n</span>: <span class="kt">usize</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nb">Vec</span><span class="o">&lt;</span><span class="kt">u8</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="n">elem</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="k">return</span><span class="w"> </span><span class="nb">Vec</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">buf</span>: <span class="nc">RawVec</span>::<span class="n">with_capacity_zeroed</span><span class="p">(</span><span class="n">n</span><span class="p">),</span><span class="w"> </span><span class="n">len</span>: <span class="nc">n</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">        </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">v</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Vec</span>::<span class="n">with_capacity</span><span class="p">(</span><span class="n">n</span><span class="p">);</span><span class="w"></span>
<span class="w">            </span><span class="n">ptr</span>::<span class="n">write_bytes</span><span class="p">(</span><span class="n">v</span><span class="p">.</span><span class="n">as_mut_ptr</span><span class="p">(),</span><span class="w"> </span><span class="n">elem</span><span class="p">,</span><span class="w"> </span><span class="n">n</span><span class="p">);</span><span class="w"></span>
<span class="w">            </span><span class="n">v</span><span class="p">.</span><span class="n">set_len</span><span class="p">(</span><span class="n">n</span><span class="p">);</span><span class="w"></span>
<span class="w">            </span><span class="n">v</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>I saw this is only applicable for <code>u8</code> but not for other integers like <code>u16</code> and friends in <code>src/liballoc/raw_vec.rs</code>, is there a reason for it?</p>



<a name="201292585"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/SpecForElem%20for%20other%20integers/near/201292585" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/SpecForElem.20for.20other.20integers.html#201292585">(Jun 18 2020 at 16:29)</a>:</h4>
<p><code>u8</code> is nice because it can use the equivalent of <code>memset</code></p>



<a name="201292656"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/SpecForElem%20for%20other%20integers/near/201292656" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/SpecForElem.20for.20other.20integers.html#201292656">(Jun 18 2020 at 16:30)</a>:</h4>
<p>I guess <code>i8</code> could cast into the same path</p>



<a name="201292820"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/SpecForElem%20for%20other%20integers/near/201292820" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/SpecForElem.20for.20other.20integers.html#201292820">(Jun 18 2020 at 16:31)</a>:</h4>
<p>other integers can only do this when they're repeating the same byte - 0, 0xFFFF, 0xABAB, ...</p>



<a name="201296513"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/SpecForElem%20for%20other%20integers/near/201296513" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/SpecForElem.20for.20other.20integers.html#201296513">(Jun 18 2020 at 16:57)</a>:</h4>
<p>u16 could specialize into something that does <code>rep stosw</code>.</p>



<a name="201296557"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/SpecForElem%20for%20other%20integers/near/201296557" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/SpecForElem.20for.20other.20integers.html#201296557">(Jun 18 2020 at 16:57)</a>:</h4>
<p>And likewise u32 and <code>rep stosd</code>.</p>



<a name="201296694"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/SpecForElem%20for%20other%20integers/near/201296694" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/SpecForElem.20for.20other.20integers.html#201296694">(Jun 18 2020 at 16:58)</a>:</h4>
<p>And u64 and <code>rep stosq</code>.</p>



<a name="201455271"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/SpecForElem%20for%20other%20integers/near/201455271" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pickfire <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/SpecForElem.20for.20other.20integers.html#201455271">(Jun 20 2020 at 01:13)</a>:</h4>
<p>So I can add one for <code>i8</code>?</p>



<a name="201455347"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/SpecForElem%20for%20other%20integers/near/201455347" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pickfire <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/SpecForElem.20for.20other.20integers.html#201455347">(Jun 20 2020 at 01:15)</a>:</h4>
<p>Also, how do I reproduce <code>rep stosw</code>? Do I add anything? Do I still need to check for the same byte?</p>



<a name="201462057"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/SpecForElem%20for%20other%20integers/near/201462057" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/SpecForElem.20for.20other.20integers.html#201462057">(Jun 20 2020 at 04:52)</a>:</h4>
<p><span class="user-mention" data-user-id="225422">@pickfire</span> I don't know offhand how to get the compiler to generate a <code>rep stosw</code> (or <code>d</code> or <code>q</code>). (And in any case, it would need benchmarking to ensure that it's actually faster.)</p>



<a name="201577448"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/SpecForElem%20for%20other%20integers/near/201577448" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pickfire <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/SpecForElem.20for.20other.20integers.html#201577448">(Jun 22 2020 at 08:07)</a>:</h4>
<p>Do we need to use <code>asm!</code> for this?</p>



<a name="201627630"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/SpecForElem%20for%20other%20integers/near/201627630" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/SpecForElem.20for.20other.20integers.html#201627630">(Jun 22 2020 at 16:20)</a>:</h4>
<p>I don't know if that would be a performance win, though it might be worth benchmarking (via a cfg-guarded specialization).</p>



<a name="201696670"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/SpecForElem%20for%20other%20integers/near/201696670" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pickfire <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/SpecForElem.20for.20other.20integers.html#201696670">(Jun 23 2020 at 07:54)</a>:</h4>
<p>cfg-guarded specialization?</p>



<a name="201746493"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/SpecForElem%20for%20other%20integers/near/201746493" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/SpecForElem.20for.20other.20integers.html#201746493">(Jun 23 2020 at 16:06)</a>:</h4>
<p>You could only do the specialization on a platform where the assembly is valid.</p>



<a name="201846004"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/SpecForElem%20for%20other%20integers/near/201846004" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pickfire <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/SpecForElem.20for.20other.20integers.html#201846004">(Jun 24 2020 at 13:22)</a>:</h4>
<p>Maybe I should just add a TODO there so others could work on it later on, I wouldn't be looking into that.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>